"""
给定一个二叉树，返回其按层次遍历的节点值。 （即逐层地，从左到右访问所有节点）。

例如:
给定二叉树: [3,9,20,null,null,15,7],
    3
   / \
  9  20
    /  \
   15   7

"""


class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None


class Solution:
    def levelOrder(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """
        res = []
        if not root: return res
        cur = [root]
        while cur:
            nexts = []
            vals = []
            for x in cur:
                if not x:continue
                vals.append(x.val)
                if x.left: nexts.append(x.left)
                if x.right: nexts.append(x.right)
            cur = nexts
            res.append(vals)
        return res